//
//  AppDelegate.m
//  OASystem
//
//  Created by macos on 14-4-15.
//  Copyright (c) 2014年 ___FULLUSERNAME___. All rights reserved.
//

#import "AppDelegate.h"
//#import "LoadView.h"
#import "LoginView.h"

@implementation AppDelegate
@synthesize loading;

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    // Override point for customization after application launch.
    
    //添加通知
    [[NSNotificationCenter defaultCenter]
     addObserver:self selector:@selector(loadWaitView) name:@"LOAD" object:nil];
    [[NSNotificationCenter defaultCenter]
     addObserver:self selector:@selector(removeLoad) name:@"REMOVE" object:nil];
    [[NSNotificationCenter defaultCenter]
     addObserver:self selector:@selector(tip:) name:@"TIP" object:nil];
    
    [[NSUserDefaults standardUserDefaults] setObject:@"" forKey:@"temp"];
    
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    LoginView *instance = [[LoginView alloc] init];
    UINavigationController *navi = [[UINavigationController alloc] initWithRootViewController:instance];
    navi.navigationBar.translucent = NO;
    self.window.rootViewController = navi;
    
    [self.window makeKeyAndVisible];
    
    return YES;
}

//加载界面
- (void)loadWaitView
{
    if(indicatorView)
        indicatorView = nil;
    indicatorView                      = [[UIView alloc] initWithFrame:INDICATOR_VIEW_FRAME];
    indicatorView.backgroundColor      = [UIColor clearColor];//LOAD_BACK_COLOR;
    UIActivityIndicatorView *indicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
    indicator.frame = CGRectMake(26, 18, 32, 32);
    
    UIImageView *indicatorBackView = [[UIImageView alloc] initWithFrame:CGRectMake(118, (self.window.frame.size.height-68)/2, 84, 68)];
    indicatorBackView.userInteractionEnabled = YES;
    indicatorBackView.image = [UIImage imageNamed:@"juhua_back.png"];
    [indicatorBackView addSubview:indicator];
    
    [indicatorView  addSubview:indicatorBackView];
    [self.window    addSubview:indicatorView];
    [indicator      startAnimating];
    
    
//    UIButton *loadButton = [UIButton buttonWithType:UIButtonTypeCustom];
//    [loadButton setBackgroundImage:[UIImage imageNamed:@""] forState:UIControlStateNormal];
//    loadButton.frame = CGRectMake(192, (self.window.frame.size.height-68)/2-10, 20, 20);
//    [loadButton addTarget:self action:@selector(cancelLoadData) forControlEvents:UIControlEventTouchUpInside];
//    [indicatorView addSubview:loadButton];
}

- (void)cancelLoadData
{
    app.loading = 1;
    
    NSLog(@"app.loading---->%d",app.loading);
    [self removeLoad];
}

- (void)removeLoad
{
    if(indicatorView)
        [indicatorView removeFromSuperview];
}

//提醒方法
- (void)tip:(NSNotification *)noti
{
    id a = [noti object];
    NSString *msg = [a objectForKey:@"msg"];
    
    if(tipView)
        [tipView removeFromSuperview];
    
    int line = msg.length>20?2:1;
    
    tipView = [[UIView alloc] initWithFrame:CGRectMake(10, [[UIScreen mainScreen] bounds].size.height - 60, 300, 40+26*(line-1))];
    tipView.backgroundColor = [UIColor clearColor];
    
    UIImageView *image = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 300, 40+26*(line-1))];
    image.backgroundColor = [UIColor blackColor];
    image.alpha = 0.6;
    [tipView addSubview:image];
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 7, 300, 26*line)];
    label.text = msg;
    label.numberOfLines = line;
    label.backgroundColor = [UIColor clearColor];
    label.textAlignment = NSTextAlignmentCenter;
    label.font = HEIGHT_FONT(14.0f);
    label.textColor = [UIColor whiteColor];
    [tipView addSubview:label];
    
    [self.window addSubview:tipView];
    
    [self performSelector:@selector(cancelAll) withObject:nil afterDelay:DELAY_TIME];
}

- (void)cancelAll
{
    if(tipView)
        [tipView removeFromSuperview];
}

							
- (void)applicationWillResignActive:(UIApplication *)application
{
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application
{
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application
{
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application
{
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application
{
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

@end
